using System;
using System.Runtime.InteropServices;
using System.Security.Permissions;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Security;
using Microsoft.SharePoint.Publishing;
using Microsoft.SharePoint.Navigation;
using PepsiCo.Common.Entities;
using System.Reflection;
using T2V.SharePoint.Common.Utilities;
using T2V.SharePoint.Common.Helpers;
using T2V.SharePoint.Common.Extensions;
using PepsiCo.Common;
using Microsoft.SharePoint.Publishing.Navigation;

namespace PepsiCo.Intranet.Features.PepsiCo.Web.Data
{
    /// <summary>
    /// This class handles events raised during feature activation, deactivation, installation, uninstallation, and upgrade.
    /// </summary>
    /// <remarks>
    /// The GUID attached to this class may be used during packaging and should not be modified.
    /// </remarks>

    [Guid("f4477b00-1811-47e6-8cb9-85a446a1b8fd")]
    public class PepsiCoWebDataEventReceiver : SPFeatureReceiver
    {
        // Uncomment the method below to handle the event raised after a feature has been activated.

        public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            var web = properties.Feature.Parent as SPWeb;

            ProvisionQuickLaunch(web);
        }


        // Uncomment the method below to handle the event raised before a feature is deactivated.

        //public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
        //{
        //}


        // Uncomment the method below to handle the event raised after a feature has been installed.

        //public override void FeatureInstalled(SPFeatureReceiverProperties properties)
        //{
        //}


        // Uncomment the method below to handle the event raised before a feature is uninstalled.

        //public override void FeatureUninstalling(SPFeatureReceiverProperties properties)
        //{
        //}

        // Uncomment the method below to handle the event raised when a feature is upgrading.

        //public override void FeatureUpgrading(SPFeatureReceiverProperties properties, string upgradeActionName, System.Collections.Generic.IDictionary<string, string> parameters)
        //{
        //}

        private void ProvisionQuickLaunch(SPWeb web)
        {
            bool tempAllowUnsafeUpdates = web.AllowUnsafeUpdates;

            web.AllowUnsafeUpdates = true;
            try
            {
                PublishingWeb publishingWeb = PublishingWeb.GetPublishingWeb(web);
                SPNavigationNodeCollection nodes = publishingWeb.Navigation.CurrentNavigationNodes;

                // clearing old quicklaunch navigation nodes
                for (int i = nodes.Count - 1; i >= 0; i--)
                {
                    nodes[i].Delete();
                }

                // setting quick launch navigation nodes
                Assembly assembly = Assembly.GetExecutingAssembly();
                string xml = assembly.GetResourceTextFile("PepsiCo.Intranet.XMLCustomSettings.CustomQuickLaunch_DepartmentSite.xml");

                var quickLaunchItems = SerializationHelper.DeserializeFromXml<CustomQuickLaunchItemDefinitionCollection>(xml);

                foreach (var navItem in quickLaunchItems)
                {
                    SPNavigationNode navNode = SPNavigationSiteMapNode.CreateSPNavigationNode(
                        navItem.Title,
                        web.Url.TrimEnd('/') + "/" + navItem.Hyperlink.TrimStart('/'),
                        navItem.IsHeading ? NodeTypes.Heading : NodeTypes.AuthoredLinkPlain,
                        nodes);

                    if (navItem.IsHeading)
                    {
                        foreach (var link in navItem.Links)
                        {
                            SPNavigationNode navSubNode = SPNavigationSiteMapNode.CreateSPNavigationNode(
                            link.Title,
                            web.Url.TrimEnd('/') + "/" + link.Hyperlink.TrimStart('/'),
                            NodeTypes.AuthoredLinkPlain,
                            navNode.Children);
                            navSubNode.Update();
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Utility.LogError(ex.Message, "PepsiCoWebDataEventReceiver ProvisionQuickLaunch");
            }
            finally
            {
                web.AllowUnsafeUpdates = tempAllowUnsafeUpdates;
            }
        }
    }
}
